﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.PublicAffair.Data;
using CentralOffice.PublicAffair.BusinessActions;
using PostTable = CentralOffice.PublicAffair.Data.PostDataSet.PostsDataTable;
using PostRow = CentralOffice.PublicAffair.Data.PostDataSet.PostsRow;

using PostReplyTable = CentralOffice.PublicAffair.Data.PostReplyDataSet.PostReplyDataTable;
using PostReplyRow = CentralOffice.PublicAffair.Data.PostReplyDataSet.PostReplyRow;

namespace CentralOffice.WebClient.PublicAffairs
{
    public partial class PostReply : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string postId = HttpContext.Current.Request.QueryString["postid"];
                PostRow post = CentralOffice.PublicAffair.BusinessActions.Post.GetById(new Guid(postId));
                if (post != null)
                {
                    lblName.Text = post.Name;
                    lblAuthor.Text = post.Author;
                    lblAccepter.Text = post.Accepter;
                    lblContentText.Text = post.ContentText;
                    //hlAttachments.Text = post.Attachments;
                    lblCreatedTime.Text = post.CreatedTime.ToString();

                    if (post.Accepter == "*")
                    {
                        lblAccepter.Text = "所有人";
                    }


                    if (post["Attachments"] != DBNull.Value && post.Attachments.Length > 0)
                    {
                        SiteUtil.GenernateDownloadHyperLink(hlAttachments, post.Attachments);
                        //string[] attachments = task.Attachments.Split('|');
                        //this.ViewState["Attachments"] = attachments;
                        //Repeater1.DataSource = attachments;
                        //Repeater1.DataBind();
                    }

                    string id = HttpContext.Current.Request.QueryString["id"];
                    if (id != null && HttpContext.Current.Request.QueryString["op"] == "reply")
                    {
                        PostReplyRow reply = CentralOffice.PublicAffair.BusinessActions.PostReply.GetById(new Guid(id));
                        if (reply != null)
                        {
                            txtName.Text = reply.ReplyTitle;
                            txtMessage.Value = reply.ReplyText;
                        }
                    }
                }
            }

        }

        protected void btnReplay_Click(object sender, EventArgs e)
        {
            string postId = HttpContext.Current.Request.QueryString["postid"];

            PostReplyDataSet replyDataSet = null;

            if (HttpContext.Current.Request.QueryString["op"] == "reply")
            {
                replyDataSet = GetReplyRow(false, new Guid(postId));
            }
            else
            {
                replyDataSet = GetReplyRow( true, new Guid(postId));
            }

            CentralOffice.PublicAffair.BusinessActions.PostReply.Update(replyDataSet);

            txtMessage.Value = "";
            txtName.Text = "";

            Response.Redirect("PostView.aspx?id=" + postId);
        }

        private PostReplyDataSet GetReplyRow(bool isNew, Guid postId)
        {
            PostReplyDataSet replyDataSet = null;

            PostReplyRow row = null;
            if (isNew)
            {
                replyDataSet = new PostReplyDataSet();
                row = replyDataSet.PostReply.NewPostReplyRow();
                row.Id = Guid.NewGuid();

                row.PostId = postId;
                row.ReplyBy = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
                row.ReplyDate = DateTime.Now;
                row.ReplyText = txtMessage.Value;
                row.ReplyTitle = txtName.Text;

                replyDataSet.PostReply.AddPostReplyRow(row);
            }
            else
            {
                row = CentralOffice.PublicAffair.BusinessActions.PostReply.GetById(new Guid(Request.QueryString["id"]));

                //row.PostId = postId;
                row.ReplyBy = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
                row.ReplyDate = DateTime.Now;
                row.ReplyText = txtMessage.Value;
                row.ReplyTitle = txtName.Text;

                replyDataSet = row.Table.DataSet as PostReplyDataSet;
            }

            string fileName = SaveFile(SiteUtil.GetPostPath(), FileUpload1);
            if (fileName.Length > 0)
            {
                row.Attachments = fileName;
            }

            return replyDataSet;
        }

        private string SaveFile(string rootDir, FileUpload file)
        {
            string fileName = "";

            string path = Server.MapPath(rootDir);
            if (file.HasFile)
            {
                //file.SaveAs(path + file.FileName);
                //fileName = rootDir + file.FileName;
                var fn = SiteUtil.GetUploadFileName(path, file.FileName);
                file.SaveAs(fn);
                fileName = rootDir + fn.Substring(fn.LastIndexOf('\\') + 1);
            }
            else
            {

            }
            return fileName;
        }
    }
}
